/*
 * Copyright 2024-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import js from '@eslint/js'
import vue from 'eslint-plugin-vue'
import typescript from '@vue/eslint-config-typescript'
import prettier from '@vue/eslint-config-prettier'
import unusedImports from 'eslint-plugin-unused-imports'
import tsParser from '@typescript-eslint/parser'
import vueParser from 'vue-eslint-parser'
import globals from 'globals'

const isProduction = process.env.NODE_ENV === 'production'

export default [
  // Ignore patterns
  {
    ignores: [
      '**/node_modules/**',
      '**/dist/**',
      '**/dist-ssr/**',
      '**/coverage/**',
      '**/build/**',
      '**/out/**',
      '**/ui/**',
      '**/.vscode/**',
      '**/.idea/**',
      '**/*.local',
      '**/cypress/videos/**',
      '**/cypress/screenshots/**',
      '**/*.tsbuildinfo',
      '**/*.log',
      '**/.env*',
      '**/Thumbs.db',
      '**/*.suo',
      '**/*.ntvs*',
      '**/*.njsproj',
      '**/*.sln',
      '**/*.sw?',
      '**/.eslintrc.cjs', // Legacy ESLint config file
    ],
  },
  // Base config for all files
  {
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2022,
      },
    },
    rules: {
      'no-console': isProduction ? 'warn' : 'off',
      'no-debugger': isProduction ? 'warn' : 'off',
      'no-prototype-builtins': 'warn',
      'prefer-const': 'warn',
    },
  },
  // Apply base recommended rules
  js.configs.recommended,
  // Vue files config
  ...vue.configs['flat/essential'],
  // TypeScript config
  ...typescript(),
  // Prettier config (skip formatting)
  prettier,
  // Unused imports plugin
  {
    plugins: {
      'unused-imports': unusedImports,
    },
    rules: {
      '@typescript-eslint/no-unused-vars': 'off', // Use unused-imports instead
      'unused-imports/no-unused-imports': 'error',
      'unused-imports/no-unused-vars': [
        'warn',
        { vars: 'all', varsIgnorePattern: '^_', args: 'after-used', argsIgnorePattern: '^_' },
      ],
    },
  },
  // TypeScript/Vue files with project
  {
    files: ['*.ts', '*.tsx', '*.vue'],
    languageOptions: {
      parser: vueParser,
      parserOptions: {
        parser: tsParser,
        ecmaVersion: 'latest',
        sourceType: 'module',
        project: './tsconfig.app.json',
        extraFileExtensions: ['.vue'],
      },
    },
    rules: {
      // TypeScript specific rules that require project - keep as warnings for better DX
      '@typescript-eslint/prefer-nullish-coalescing': 'warn',
      '@typescript-eslint/prefer-optional-chain': 'warn',
      '@typescript-eslint/no-unnecessary-condition': 'warn',
      'vue/multi-word-component-names': 'off',
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-inferrable-types': 'off',
      // Enhanced rules for better error detection during refactoring
      'vue/no-undef-properties': 'error',
      'vue/no-unused-properties': 'warn',
      'vue/no-unused-refs': 'warn',
      'vue/require-prop-types': 'error',
      'vue/require-default-prop': 'warn',
      'vue/no-unused-emit-declarations': 'warn',
      'vue/no-use-v-if-with-v-for': 'warn',
    },
  },
  // JavaScript files
  {
    files: ['*.js', '*.cjs', '*.mjs'],
    languageOptions: {
      globals: {
        ...globals.node,
      },
    },
    rules: {
      // Disable TypeScript-specific rules for JS files
      '@typescript-eslint/no-var-requires': 'off',
    },
  },
  // Config files
  {
    files: ['*.config.ts', '*.config.js', 'cypress/**/*', 'vite.config.ts', 'vitest.config.ts'],
    languageOptions: {
      globals: {
        ...globals.node,
      },
      parserOptions: {
        // Don't use project for config files
        project: null,
      },
    },
    rules: {
      // Disable TypeScript-specific rules for config files
      '@typescript-eslint/prefer-nullish-coalescing': 'off',
      '@typescript-eslint/prefer-optional-chain': 'off',
      '@typescript-eslint/no-unnecessary-condition': 'off',
    },
  },
]
